import { request } from 'ice';

/**
 * 生成api
 * @param api 
 * @returns 
 */
export function generateApi<T extends ApiConfig>(api: T): {
  [Key in keyof T]: (params?: JSONObject, option?: JSONObject) => Promise<any>;
} {
  const Api: {
    [Key in keyof T]: (params?: JSONObject, option?: JSONObject) => Promise<any>;
  } = {} as {
    [Key in keyof T]: (params?: JSONObject, option?: JSONObject) => Promise<any>;
  };
  for (const key in api) {
    const { url, data, ...options } = api[key];
    Api[key] = async (params: JSONObject = {}, option: JSONObject = {}) => {
      const result = await request.post(url, { ...params, ...data }, { ...option, ...options });
      if (result) {
        return result?.data?.data;
      }
      return result;
    };
  }
  return Api;
}